package com.onlyyou.custom.handle;

import com.onlyyou.custom.VO.ResultVO;
import com.onlyyou.custom.enums.ResultEnum;
import com.onlyyou.custom.exception.CustomException;
import com.onlyyou.custom.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: 小野
 * @Date: 2020/1/12 17:19
 * 捕获异常
 */
@RestControllerAdvice
@Slf4j
public class CustomHandle {
    @ExceptionHandler(value = RuntimeException.class)
    public ResultVO handle(Exception e) {
        if (e instanceof CustomException) {
            CustomException cartException= (CustomException) e;
            return ResultUtil.error(cartException.getCode(),e.getMessage());
        } else if(e instanceof UnknownAccountException){
            return ResultUtil.error(ResultEnum.NULL.getStatus(),e.getMessage());
        }
        return ResultUtil.error(500,e.getMessage());
    }
}
